<?php
if ( !defined( "IN_OP" ) )
{
    exit( "You can't access this file directly..." );
}
$conf =new Config();
$rate0a_b = floatval( $conf->get( "rate0a_b" ) );
$rate0a_c = floatval( $conf->get( "rate0a_c" ) );
$rate0a_d = floatval( $conf->get( "rate0a_d" ) );
$rate1a_b = floatval( $conf->get( "rate1a_b" ) );
$rate1a_c = floatval( $conf->get( "rate1a_c" ) );
$rate1a_d = floatval( $conf->get( "rate1a_d" ) );
$ui = new ui();
$tpl = new Template2();
$tpl->load( "main", "srate20.htm" );
$tpl->set( "cchinayear", intval( $conf->get( "chinayear" ) ) );
$disk = "a";
if ( isset( $_GET['disk'] ) )
{
    $disk = $_GET['disk'];
}
else if ( isset( $_POST['disk'] ) )
{
    $disk = $_POST['disk'];
}
$fieldArr = array( "method" => "5", "gametype" => "0", "round" => "0", "lotto_id" => "0", "betcontent" => "", "orate" => "", "nrate" => "", "pagename" => "2", "paperopen" => date( "Y-m-d" ) );
include( "admin_menu.php" );
include( "rate_menu.php" );
srateMenu( "srate20", $disk );
$tpl->set( "action", "op.php?op=admin&fp=srate20&disk=".$disk );
if ( $disk == "a" )
{
    $setRateFlag = true;
}
else
{
    $setRateFlag = false;
}
if ( ispost( ) )
{
    $save_error = false;
    $fields = array();
    if ( $setRateFlag )
    {
        $sqlRate = "!!SELECT * FROM rate3a WHERE lotto_id = 0 LIMIT 0,1";
        $rsRate = getdb( $sqlRate );
    }
    $i = 1;
    while ( $i < 50 )
    {
        $ii = d2( $i );
        $temp = $_POST["r0_".$ii];
        $idx = "r".$ii;
        $fields[$idx] = floatval( $temp );
        if ( $setRateFlag && !$rsRate->eof && $rsRate->f[$idx] != $fields[$idx] )
        {
            $fieldArr['betcontent'] = GetBetContent( 4, $ii );
            $fieldArr['orate'] = $rsRate->f[$idx];
            $fieldArr['nrate'] = $fields[$idx];
            setRateRecord( $fieldArr );
        }
        ++$i;
    }
    if ( !$save_error )
    {
        db_update( "rate3".$disk, $fields, "lotto_id=0" );
    }
}
$rs = getdb( "!!select * from rate3{$disk} where lotto_id=0" );
if ( $rs->eof )
{
    getdb( "insert into rate3{$disk} (lotto_id) values (0)" );
    $rs = getdb( "!!select * from rate3{$disk} where lotto_id=0" );
}
$i = 1;
while ( $i < 50 )
{
    $ii = d2( $i );
    $tpl->set( "r0_".$ii, trimfloat( $rs->f["r".$ii], 1 ) );
    ++$i;
}
$rs->close();
if ( ispost( ) )
{
    $fields = array();
    if ( $setRateFlag )
    {
        $sqlRate = "!!SELECT * FROM rate16a WHERE lotto_id = 0 LIMIT 0,1";
        $rsRate = getdb( $sqlRate );
    }
    $i = 1;
    while ( $i <= 6 )
    {
        $j = 1;
        while ( $j <= 9 )
        {
            $idx = "r15_{$i}_{$j}";
            $temp = floatval( $_POST[$idx] );
            if ( $temp < 10 )
            {
                $fields[$idx] = $temp;
                if ( $setRateFlag && !$rsRate->eof && $rsRate->f[$idx] != $fields[$idx] )
                {
                    $fieldArr['betcontent'] = GetBetContentForSetRateRecord( $idx );
                    $fieldArr['orate'] = $rsRate->f[$idx];
                    $fieldArr['nrate'] = $fields[$idx];
                    setRateRecord( $fieldArr );
                }
            }
            ++$j;
        }
        ++$i;
    }
    db_update( "rate16".$disk, $fields, "lotto_id=0" );
    if ( $disk == "a" )
    {
        if ( $rate1a_b != 0 )
        {
            $fields = array();
            $i = 1;
            while ( $i <= 6 )
            {
                $j = 1;
                while ( $j <= 4 )
                {
                    $fields["r15_".$i."_".$j] = floatval( $_POST["r15_".$i."_".$j] ) - $rate1a_b;
                    ++$j;
                }
                $fields["r15_".$i."_5"] = floatval( $_POST["r15_".$i."_5"] );
                $fields["r15_".$i."_6"] = floatval( $_POST["r15_".$i."_6"] );
                $fields["r15_".$i."_7"] = floatval( $_POST["r15_".$i."_7"] );
                $fields["r15_".$i."_8"] = floatval( $_POST["r15_".$i."_8"] ) - $rate1a_b;
                $fields["r15_".$i."_9"] = floatval( $_POST["r15_".$i."_9"] ) - $rate1a_b;
                ++$i;
            }
            db_update( "rate16b", $fields, "lotto_id=0" );
        }
        if ( $rate1a_c != 0 )
        {
            $fields = array();
            $i = 1;
            while ( $i <= 6 )
            {
                $j = 1;
                while ( $j <= 4 )
                {
                    $fields["r15_".$i."_".$j] = floatval( $_POST["r15_".$i."_".$j] ) - $rate1a_c;
                    ++$j;
                }
                $fields["r15_".$i."_5"] = floatval( $_POST["r15_".$i."_5"] );
                $fields["r15_".$i."_6"] = floatval( $_POST["r15_".$i."_6"] );
                $fields["r15_".$i."_7"] = floatval( $_POST["r15_".$i."_7"] );
                $fields["r15_".$i."_8"] = floatval( $_POST["r15_".$i."_8"] ) - $rate1a_c;
                $fields["r15_".$i."_9"] = floatval( $_POST["r15_".$i."_9"] ) - $rate1a_c;
                ++$i;
            }
            db_update( "rate16c", $fields, "lotto_id=0" );
        }
        if ( $rate1a_d != 0 )
        {
            $fields = array();
            $i = 1;
            while ( $i <= 6 )
            {
                $j = 1;
                while ( $j <= 4 )
                {
                    $fields["r15_".$i."_".$j] = floatval( $_POST["r15_".$i."_".$j] ) - $rate1a_d;
                    ++$j;
                }
                $fields["r15_".$i."_5"] = floatval( $_POST["r15_".$i."_5"] );
                $fields["r15_".$i."_6"] = floatval( $_POST["r15_".$i."_6"] );
                $fields["r15_".$i."_7"] = floatval( $_POST["r15_".$i."_7"] );
                $fields["r15_".$i."_8"] = floatval( $_POST["r15_".$i."_8"] ) - $rate1a_d;
                $fields["r15_".$i."_9"] = floatval( $_POST["r15_".$i."_9"] ) - $rate1a_d;
                ++$i;
            }
            db_update( "rate16d", $fields, "lotto_id=0" );
        }
    }
}
$rs = getdb( "!!select * from rate16".$disk." where lotto_id=0" );
if ( $rs->eof )
{
    getdb( "insert into rate16".$disk." (lotto_id) values (0)" );
    $rs = getdb( "!!select * from rate16".$disk." where lotto_id=0" );
}
$i = 1;
while ( $i <= 6 )
{
    $j = 1;
    while ( $j <= 9 )
    {
        $tpl->set( "r15_".$i."_".$j, trimfloat( $rs->f["r15_".$i."_".$j], 1 ) );
        ++$j;
    }
    ++$i;
}
if ( ispost( ) )
{
    $fields = array();
    if ( floatval( $_POST['r5_1'] ) < 10 )
    {
        $fields['r5_1'] = floatval( $_POST['r5_1'] );
    }
    if ( floatval( $_POST['r5_2'] ) < 10 )
    {
        $fields['r5_2'] = floatval( $_POST['r5_2'] );
    }
    if ( floatval( $_POST['r6_1'] ) < 10 )
    {
        $fields['r6_1'] = floatval( $_POST['r6_1'] );
    }
    if ( floatval( $_POST['r6_2'] ) < 10 )
    {
        $fields['r6_2'] = floatval( $_POST['r6_2'] );
    }
    $fields['r9_1'] = floatval( $_POST['r9_1'] );
    $fields['r9_2'] = floatval( $_POST['r9_2'] );
    $fields['r7_1'] = floatval( $_POST['r7_1'] );
    $fields['r10_1'] = floatval( $_POST['r10_1'] );
    $fields['r10_2'] = floatval( $_POST['r10_2'] );
    $fields['r11_1'] = floatval( $_POST['r11_1'] );
    if ( $setRateFlag )
    {
        $sqlRate = "!!SELECT * FROM rate5a WHERE lotto_id = 0 LIMIT 0,1";
        $rsRate = getdb( $sqlRate );
        if ( !$rsRate->eof )
        {
            $chkArr = array( "r5_1", "r5_2", "r6_1", "r6_2", "r9_1", "r9_2","r7_1", "r10_1", "r10_2", "r11_1" );
            foreach ( $chkArr as $val )
            {
                if ( $rsRate->f[$val] != $fields[$val] )
                {
                    $fieldArr['betcontent'] = GetBetContentForSetRateRecord( $val );
                    $fieldArr['orate'] = $rsRate->f[$val];
                    $fieldArr['nrate'] = $fields[$val];
                    setRateRecord( $fieldArr );
                }
            }
        }
    }
    db_update( "rate5".$disk, $fields, "lotto_id=0" );
    if ( $disk == "a" && $rate1a_b != 0 )
    {
        $fields = array();
        $fields['r5_1'] = floatval( $_POST['r5_1'] ) - $rate1a_b;
        $fields['r5_2'] = floatval( $_POST['r5_2'] ) - $rate1a_b;
        $fields['r6_1'] = floatval( $_POST['r6_1'] ) - $rate1a_b;
        $fields['r6_2'] = floatval( $_POST['r6_2'] ) - $rate1a_b;
        $fields['r9_1'] = floatval( $_POST['r9_1'] );
        $fields['r9_2'] = floatval( $_POST['r9_2'] );
        $fields['r7_1'] = floatval( $_POST['r7_1'] );
        $fields['r10_1'] = floatval( $_POST['r10_1'] );
        $fields['r10_2'] = floatval( $_POST['r10_2'] );
        $fields['r11_1'] = floatval( $_POST['r11_1'] );
        db_update( "rate5b", $fields, "lotto_id=0" );
    }
    if ( $disk == "a" && $rate1a_c != 0 )
    {
        $fields = array();
        $fields['r5_1'] = floatval( $_POST['r5_1'] ) - $rate1a_c;
        $fields['r5_2'] = floatval( $_POST['r5_2'] ) - $rate1a_c;
        $fields['r6_1'] = floatval( $_POST['r6_1'] ) - $rate1a_c;
        $fields['r6_2'] = floatval( $_POST['r6_2'] ) - $rate1a_c;
        $fields['r9_1'] = floatval( $_POST['r9_1'] );
        $fields['r9_2'] = floatval( $_POST['r9_2'] );
        $fields['r7_1'] = floatval( $_POST['r7_1'] );
        $fields['r10_1'] = floatval( $_POST['r10_1'] );
        $fields['r10_2'] = floatval( $_POST['r10_2'] );
        $fields['r11_1'] = floatval( $_POST['r11_1'] );
        db_update( "rate5c", $fields, "lotto_id=0" );
    }
    if ( $disk == "a" && $rate1a_d != 0 )
    {
        $fields = array();
        $fields['r5_1'] = floatval( $_POST['r5_1'] ) - $rate1a_d;
        $fields['r5_2'] = floatval( $_POST['r5_2'] ) - $rate1a_d;
        $fields['r6_1'] = floatval( $_POST['r6_1'] ) - $rate1a_d;
        $fields['r6_2'] = floatval( $_POST['r6_2'] ) - $rate1a_d;
        $fields['r9_1'] = floatval( $_POST['r9_1'] );
        $fields['r9_2'] = floatval( $_POST['r9_2'] );
        $fields['r7_1'] = floatval( $_POST['r7_1'] );
        $fields['r10_1'] = floatval( $_POST['r10_1'] );
        $fields['r10_2'] = floatval( $_POST['r10_2'] );
        $fields['r11_1'] = floatval( $_POST['r11_1'] );
        db_update( "rate5d", $fields, "lotto_id=0" );
    }
}
$rs = getdb( "!!select * from rate5".$disk." where lotto_id=0" );
if ( $rs->eof )
{
    getdb( "insert into rate5".$disk." (lotto_id) values (0)" );
    $rs = getdb( "!!select * from rate5".$disk." where lotto_id=0" );
}
$tpl->set( "r5_1", trimfloat( $rs->f['r5_1'], 1 ) );
$tpl->set( "r5_2", trimfloat( $rs->f['r5_2'], 1 ) );
$tpl->set( "r6_1", trimfloat( $rs->f['r6_1'], 1 ) );
$tpl->set( "r6_2", trimfloat( $rs->f['r6_2'], 1 ) );
$tpl->set( "r9_1", floatval( $rs->f['r9_1'] ) );
$tpl->set( "r9_2", floatval( $rs->f['r9_2'] ) );
$tpl->set( "r7_1", floatval( $rs->f['r7_1'] ) );
$tpl->set( "r10_1", floatval( $rs->f['r10_1'] ) );
$tpl->set( "r10_2", floatval( $rs->f['r10_2'] ) );
$tpl->set( "r11_1", floatval( $rs->f['r11_1'] ) );
$tpl->show( "main" );
?>